<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-waiting-current-waiting-lineup" :style="getStyle('area')">
      <div class="patient-area">
        <div class="current-area" :style="getStyle('currentTitle')">
          <div class="title" :style="getStyle('currentTitleSty')">
            <ScrollX>{{ optionsInfo["currentPaperwork"] }}</ScrollX>
          </div>
          <div class="content">
            <vue-seamless-scroll
                ref="current_scroll"
                class="scroll"
                :class-option="classOptionTips()"
                :data="waitingPatientInfos"
                :key="`current_${widgetKey}`"
            >
              <div class="scroll-box">
                <template v-for="(itemInfo, index) in waitingPatientInfos">
                  <div class="item" :key="`${itemInfo.orderNo}-${index}`" :style="getStyle('waitingContent')">
                    <div class="ellipsis vertical-center">
                      {{ itemInfo.treatmentRoomName || "\u00a0" }}
                    </div>
                    <div class="content-patient-info" v-html="currPatientInfo(itemInfo)"></div>
                  </div>
                </template>
              </div>
            </vue-seamless-scroll>
          </div>
        </div>
        <div class="waiting-area">
          <div class="title" :style="getStyle('waitingTitle')">
            <ScrollX>{{ optionsInfo["waitingPaperwork"] }}{{ optionsInfo['waitingVisitStep'] }}</ScrollX>
          </div>

          <div class="content" :data-step="optionsInfo['waitingVisitStep']">
            <vue-seamless-scroll
                ref="wait_scroll"
                class="scroll"
                :key="`wait_${widgetKey}`"
                :data="waitPatientInfo"
                :class-option="classWaitOptionTips()"
                style="overflow: hidden; width: 100%; text-align: center"
            >
              <div
                  class="content-inside"
                  v-for="(itemInfo, index) in waitPatientInfo"
              >
                <div
                    class="content-inside-child"
                    v-for="(itemInfo2, itemInfoKey2) in itemInfo"
                    :key="`${itemInfo2.queuingId}-${itemInfoKey2}`"
                    :style="getStyle('waitingTitle')"
                >
                  <div class="waiting-pat">
                    <div class="child-inside" v-if="true">
                      {{ itemInfo2.orderNo || "\u00a0" }}{{ itemInfo2.patientName || "\u00a0" }}
                    </div>
                  </div>
                </div>
              </div>
            </vue-seamless-scroll>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import vueSeamlessScroll from "vue-seamless-scroll";
import { buildParentApi } from "@/utils/designer/util";
import ScrollY from "@c/public/seamlessScroll/ScrollY.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
let buildParent = buildParentApi();
export default {
  name: "scene-waiting-current-waiting-lineup-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollY,
    CompOperate,
    ScrollX,
    vueSeamlessScroll,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    };
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.optionsInfo || Array();
        let Obj = Object();
        switch (val) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(info.areaHeight));
            return Obj;
          case "currentTitle":
            Reflect.set(Obj, "fontSize", pxToVw(info.currentFontSize || 50));
            Reflect.set(Obj, "color", info.headerFontColor || "#fffff");
            Reflect.set(
                Obj,
                "backgroundColor",
                info.headerBackgroundColor || "#0059b3"
            );
            return Obj;
          case "waitingContent":
            Reflect.set(Obj, "color", info.headerFontColor || "#fffff");
            return Obj;
          case "currentTitleSty":
            Reflect.set(Obj, "fontSize", pxToVw(info.currentFontSize || 50));
            return Obj;
          case "waitingTitle":
            Reflect.set(Obj, "fontSize", pxToVw(info.waitingFontSize || 50));
            return Obj;
        }
      };
    },

    // 正在就诊滚动
    classOptionTips() {
      return function () {
        return {
          step: 2, // 数值越大速度滚动越快
          hoverStop: true,
          limitMoveNum: 4, // 开始无缝滚动的数据量 this.dataList.length
          direction: 2, // 0向下 1向上 2向左 3向右
        };
      }
    },

    // 队列的当前患者
    currPatientInfo(){
      return function (val) {
        /**
         * 在线状态 0在线 1暂停 2退出
         **/
        const self = this;
        let type = parseInt(val.onlineStatus);
        switch (type) {
          case 0:
          case 2:
            if (!!(val?.treatingPatient?.length)) {
              let firstPatient = self.$lodash.head(val.treatingPatient);
              return firstPatient.patientName || '\u00a0';
            } else {
              return "\u00a0";
            }
          case 1:
            return "暂停接诊";
        }
      };


    },

    // 候诊滚动
    classWaitOptionTips() {
      return function () {
        let {openScroll, waitingVisitStep, waitingNumber} = this.optionsInfo;
        return {
          step: 2,
          hoverStop: false,
          limitMoveNum: openScroll ? waitingNumber : 99999, // 开始无缝滚动的数据量 this.dataList.length
          direction: 1, // 0向下 1向上 2向左   3向右
        };
      }
    },

    //  队列-患者-数据
    doctorPatientListInfo() {
      let data;
      let devType = this.devType;
      let WaitingPatientList = this.$store.getters["getWaitingPatientInfo"];
      if (devType) {
        data = WaitingPatientList.apiData;
      } else {
        data = WaitingPatientList.mockData;
      }
      return data || [{waitPatientList: []}];
    },

    waitPatientInfo() {
      // 等待检查患者
      let list = this.doctorPatientListInfo;
      let summary = this.$lodash.map(list, "waitPatientList");
      let flattenArr = this.$lodash.flattenDeep(summary);
      let uniqByArr = this.$lodash.uniqBy(flattenArr, "queuingId");

      if (uniqByArr.length > 16) {
        console.log(this.$lodash.chunk(uniqByArr, 4))
        return this.$lodash.chunk(uniqByArr, 4);
      } else {
        let arr = new Array(16 - uniqByArr.length).fill({});
        let flattenArr2 = [...uniqByArr, ...arr];
        return this.$lodash.chunk(flattenArr2, 4);
      }
    },

    // 列表的队列的患者(顶部)
    waitingPatientInfos() {
      let list = Array();
      let devType = this.devType;
      let WaitingPatientList = this.$store.getters["getWaitingPatientInfo"];
      if (devType) {
        list = WaitingPatientList.apiData;
      } else {
        list = WaitingPatientList.mockData;
      }
      // 不在线叫号器--剔除队伍
      list = list.filter(item => {
        return item.onlineStatus !== '2';
      })
      if (list.length < 3) {
        let arr = new Array(3 - list.length).fill({});
        list = [...list, ...arr]
      }
      return list
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          this.$store.dispatch("setWaitingPatientInfo", patientList);
          break;
      }
    },

  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = val.options.config;
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true,
    },


  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-waiting-current-waiting-lineup {
  padding: 28px 30px 10px 30px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  color: #333;
  height: auto;
  font-size: 50px;
  background: #e5eaef;

  .dept-area {
    font-weight: 500;
    font-size: 110px;
    height: 220px;
    color: #ffffff;
    line-height: 1.5;
    width: 100%;
    text-align: center;
    background: #0059b3;
    padding: 30px 0;
    overflow: hidden;
    border-radius: 32px 32px 0 0;
    // @include singleEllipsis(100%);
  }

  .patient-area {
    overflow: inherit;
    border-radius: 32px 32px;
    overflow: hidden;
    background: transparent;
    box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
    inset 5px 5px 10px rgba(255, 255, 255, 0.75),
    inset -1px -1px 10px rgba(0, 0, 0, 0.2);

    .current-area {
      display: flex;
      height: 150px;
      flex-direction: row;
      overflow: hidden;
      border-radius: 0 0 12px 12px;
      //background: rgba(0, 89, 179, 1);

      .title {
        color: white;
        font-size: 40px;
        height: 100%;
        box-sizing: border-box;
        width: 260px;
        background: rgba(0, 71, 143, 1);
        display: flex;
        padding: 0 20px;
        justify-content: center;
        align-items: center;
        align-self: center;

        .scrollText {
          width: 100%;
        }
      }

      .content {
        color: white;
        padding: 42px 0;
        display: flex;
        flex-direction: row;
        flex: 1;
        height: 100%;
        align-self: center;
        justify-content: center;
        align-items: center;
        overflow: hidden;

        .scroll {
          width: 100%;
          //display: flex;
          //justify-content: space-around;
          overflow: hidden;
        }

        .scroll-box {
          width: 100%;
          display: flex;

          .item {
            width: 533px;
            text-align: center;
            //border-right: 1px solid #ccc;
            .ellipsis {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              width: 100%;
              height: 60px;
              padding-top: 7px;
              max-width: 100%;
            }
          }
        }
      }
    }

    .waiting-area {
      display: flex;
      //margin-top: 14px;
      flex-direction: column;
      border-radius: 24px 24px 0 0;
      overflow: hidden;

      .title {
        background: linear-gradient(
                rgba(244, 244, 244, 1) 0%,
                rgba(240, 240, 240, 1) 100%
        );
        text-align: center;
        font-size: 50px;
        height: 120px;
        width: 100%;
        color: #0059b3;
        border-bottom: 2px solid #dddddd;
        // @include fontWeight();
        display: flex;
        padding: 0 20px;
        align-self: center;
        align-items: center;
        justify-content: center;

        .scrollText {
          width: 100%;
          height: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          align-self: center;
        }
      }

      .content {
        background: white;
        display: flex;
        flex-wrap: wrap;

        .content-inside {
          width: 100%;
          display: flex;
          padding: 8px 0;
          flex-wrap: wrap;

          .content-inside-child {
            font-weight: 400;
            text-align: center;
            display: flex;
            flex-basis: calc(100% / 4);
            flex-shrink: 0;
            justify-content: center;
            align-self: center;
            align-items: center;
            height: 108px;
            font-size: 54px;
            color: #333333;
            overflow: hidden;

            .child-inside {
              padding: 0 24px;
              // @include singleEllipsis(100%);
            }
          }
        }
      }

      .even {
        background: rgba(244, 244, 244, 1);
      }
    }
  }
}
</style>
